<?php
/**
 * 后台管理 - 疯狂工厂
 * 
 * @version $Id$
 */

 loadLib('App_Factory_Event');

class Module_factory
{
	/**
	 * 构造函数
	 */
	function __construct() {
		
	}


	/**
	 * 事件管理
	 */
	function event() {
		$smarty = loadSmarty();
		$Db = loadLib('Db');

		// 获取已有的事件
		$event_list = $Db->getAll("SELECT * FROM `app_factory_event` ORDER BY `id` DESC");
		foreach($event_list as $k => $event) {
			$event_list[$k]['guide'] = unserialize($event['guide']);
			$event_list[$k]['info'] = unserialize($event['info']);
			$event_list[$k]['result'] = unserialize($event['result']);
			$event_list[$k]['place_name'] = App_Factory_Event::getPlaceName($event['place']);
		}

		$smarty->assign('event_list' , $event_list);

		return $smarty->fetch('factory/event.tpl.php');
	}

	/**
	 * 添加事件界面
	 */
	function eventadd() {
		$event_id = isset($_GET['id']) ? intval($_GET['id']) : null;

		$smarty = loadSmarty();

		if(!empty($event_id)) {
			$event = App_Factory_Event::getById($event_id);

			if(!empty($event)) {
				$smarty->assign('type' , 'modify');
				$smarty->assign('event' , $event);
			}
		}


		$places = App_Factory_Event::getPlaceName();
		$smarty->assign('places' , $places);
	//print_r($event);
		return $smarty->fetch('factory/eventadd.tpl.php');
	}

	/**
	 * 添加事件
	 */
	function eventadd_do() {
		$event_id = isset($_POST['event_id']) ? intval($_POST['event_id']) : null;
		$name = isset($_POST['name']) ? trim($_POST['name']) : '';
		$place = isset($_POST['place']) ? trim($_POST['place']) : '';
		$probability = isset($_POST['probability']) ? intval($_POST['probability']) : '';

		$guide_text = isset($_POST['guide_text']) ? trim($_POST['guide_text']) : '';
		$guide_ok = isset($_POST['guide_ok']) ? trim($_POST['guide_ok']) : '';
		$guide_cancel = isset($_POST['guide_cancel']) ? trim($_POST['guide_cancel']) : '';

		$info_text = isset($_POST['info_text']) ? trim($_POST['info_text']) : '';
		$info_ok = isset($_POST['info_ok']) ? trim($_POST['info_ok']) : '';
		$info_cancel = isset($_POST['info_cancel']) ? trim($_POST['info_cancel']) : '';

		if(empty($name)) {
			return Format::json_return(1 , '没有填写 事件名');
		}

		if(empty($place)) {
			return Format::json_return(1 , '没有填写 发生地点');
		}

		if(empty($probability)) {
			return Format::json_return(1 , '没有填写 概率');
		}

		if(empty($guide_text)) {
			return Format::json_return(1 , '没有填写 引导文字');
		}

		if(empty($guide_ok)) {
			return Format::json_return(1 , '没有填写 引导下一步按钮');
		}

		if(empty($info_text)) {
			return Format::json_return(1 , '没有填写 信息文字');
		}

		if(empty($info_ok)) {
			return Format::json_return(1 , '没有填写 信息下一步按钮');
		}

		$result = array();

		// 结果
		$result_length = !empty($_POST['result_text']) ? count($_POST['result_text']) : 0;
		for($i = 0 ; $i < $result_length ; $i++) {
			if(!empty($_POST['result_text'][$i])) {
				if(empty($_POST['result_probability'][$i]) || empty($_POST['result_ok'][$i])) {
					return Format::json_return(1 , '结果' . ($i + 1) . '没有填写全');
				}

				$result[] = array(
					'text' => trim($_POST['result_text'][$i]),
					'probability' => intval($_POST['result_probability'][$i]),
					'gain_type' => trim($_POST['result_gain_type'][$i]),
					'gain_num' => intval($_POST['result_gain_num'][$i]),
					'ok' => trim($_POST['result_ok'][$i]),
				);
			}
		}

		if(empty($result)) {
			return Format::json_return(1 , '没有填写任何结果');
		}

		// 判断结果的概率的和是否是100%
		$result_probability_total = 0;
		foreach($result as $row) {
			$result_probability_total += $row['probability'];
		}
		if($result_probability_total !== 100) {
			return Format::json_return(1 , '结果的概率总和不是100 (现在是' . $result_probability_total . ')');
		}

		$guide = array(
			'text' => $guide_text,
			'ok' => $guide_ok,
			'cancel' => $guide_cancel,
		);

		$info = array(
			'text' => $info_text,
			'ok' => $info_ok,
			'cancel' => $info_cancel,
		);
		
		// 处理数据
		$Db = loadLib('Db');
		$insert_array = array(
			'name' => $name,
			'place' => $place,
			'probability' => $probability,
			'guide' => serialize( $guide ),
			'info' => serialize( $info ),
			'result' => serialize( $result ),
			'uin' => $_SESSION['adminid'],
			'time' => time(),
		);

		if(empty($event_id)) {
			$success = $Db->doInsert('app_factory_event' , $insert_array);
			$success_str = '添加成功';
		} else {
			$success = $Db->doUpdate('app_factory_event' , $insert_array , '`id` = ' . $event_id);
			$success_str = '修改成功';
		}


		if(!$success) {
			return Format::json_return(1 , '写数据库失败');
		}

		return Format::json_return(0 , $success_str);
	}


	function eventdelete() {
		$event_id = isset($_POST['event_id']) ? intval($_POST['event_id']) : null;

		if(empty($event_id)) {
			return Format::json_return(1 , '参数错误');
		}

		$Db = loadLib('Db');
		$Db->query("DELETE FROM `app_factory_event` WHERE `id` = " . $event_id);

		return Format::json_return(0 , '删除成功');
	}

}

# end of script
